<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 9:27
 */

namespace app\api\controller\product;
use app\api\controller\HomeCommon;
use app\Validate\IdMustPositive;
use app\Validate\PageMustValidate;
use think\Controller;
use think\Request;

class ProductCategory extends Controller
{
    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    //查询三级分类数据
    public function categoryList(Request $request)
    {
        (new PageMustValidate())->goCheck();
        $where = [];
        $post = $request->post();
        $where = addWhere($post, ['level' => '', 'pid' => ''], $where, 1);//等级 和 上一级的分类
        $where['page'] = $post['page'];
        $dataList = (new \app\api\model\ProductCategory())->wechatListTotal($where, "id,name,cover_img,level", "sort desc,id desc", $post['page_number']);
        return writeJson(200, $dataList);
    }
    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    //查询三级分类数据
    public function getCategoryByLevelOne(Request $request)
    {
        (new IdMustPositive())->goCheck();

        $post = $request->post();
        $where_two = [
            ['level','eq',2],
            ['pid','eq',$post['id']]
        ];
        $levelTwo = (new \app\api\model\ProductCategory())->selectList($where_two,"id,name,level","sort desc,id desc");
        $levelTwo = $levelTwo->toArray();
        $idList =   array_column($levelTwo,"id");
        $where_three = [
            ['level','eq',3],
            ['pid','in',$idList]
        ];
        $levelThree =   (new \app\api\model\ProductCategory())->selectList($where_three,"id,name,cover_img,level,pid","sort desc,id desc");
        foreach ($levelTwo as $key=>$value){
            if(!isset($value['itemList'])){
                $levelTwo[$key]['itemList'] = [];
            }
            foreach ($levelThree as $ke=>$val){
                if($val['pid']== $value['id']){
                    $levelTwo[$key]['itemList'][] = $val;
                }
            }
        }
        return writeJson(200, ['category'=>$levelTwo]);
    }
}